/*
 * Created: 2022-09-17 16:55:12
 * Author : 涛涛涛
 */

import { apiGetArtworkById } from './common/apiGetArtworkById';
import { apiGetAuthorById } from './common/apiGetAuthorById';
import { apiGetProductList } from './common/apiGetProductList';
type ApiParams = {
    artwork?: boolean;
    artist?: boolean;
};
/** 获得完整的获得完整的商品信息.
 * 取到商品数据,此处对取商品数据的方法还需要进行一次包装,因为这需要合并多个接口,并不是单个接口自己玩
 * 获得商品信息后.同时再发送请求,将作者信息,和艺术品信息也取得
 * 也可以通过配置.然后不取得这些内容
 */
export const apiGetIntactProductList = async (params: ApiParams = { artwork: true, artist: true }) => {
    const { artist = true, artwork = true } = params;
    const productRes = await apiGetProductList();
    let promiseArr = [];
    /** 如果两者的入参配置为true,则请求 */
    productRes.data.forEach((product) => {
        if (artwork === true) {
            let p1 = apiGetArtworkById(product.artworkId).then((artRes) => product.immit_artwork(artRes.data));
            promiseArr.push(p1);
        }
        if (artist === true) {
            let p2 = apiGetAuthorById(product.artistId).then((authRes) => product.immit_author(authRes.data));
            promiseArr.push(p2);
        }
    });
    await Promise.all(promiseArr);
    return productRes;
};
